//
//  HMSelectAddressView.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/11.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSelectAddressView.h"
#import "HMDefaultAddressTableViewCell.h"
#import "HMAddressModel.h"
@interface HMSelectAddressView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) UIView *alert;

@end

@implementation HMSelectAddressView

- (instancetype)initWithFrame:(CGRect)frame  {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
//        self.frame = CGRectMake(0, 0, KWidthSCREEN, KHeightSCREEN);
        [self setUpSubView];
        [self setupWithBlackView];
    }
    return self;
}

- (void)setUpSubView {
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self addSubview:_myTableView];
}

//设置弹出的动画
- (void)setupWithBlackView {
    UIView *blackView = [[UIView alloc] init];
    blackView.layer.masksToBounds = YES;
    self.alert = blackView;
    blackView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1];
    [self addSubview:blackView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMDefaultAddressTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMDefaultAddressTableViewCell" owner:self options:nil] firstObject];
    cell.addressModel = _dataArray[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_delegate respondsToSelector:@selector(select:)]) {
        [self dismiss];
        [_delegate select:_dataArray[indexPath.section]];
    }
}

- (void)show {
    _hidden = YES;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.alpha = 0;
    self.alert.transform = CGAffineTransformMakeScale(1.08, 1.08);
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.alert.transform = CGAffineTransformIdentity;
    }];
}

- (void)dismiss {
    _hidden = NO;
    [UIView animateWithDuration:0.38 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setHidden:(BOOL)hidden {
    if (hidden == YES) {
        [self show];
    } else {
        [self dismiss];
    }
}


@end
